/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author gean
 */
public class remettre extends javax.swing.JFrame {

    
    Connection conn =null;
    ResultSet rs =null;
    
    
    public remettre() {
        initComponents();
        conn=gestion_chambes_hotel.Gestion_Chambes_Hotel.DB_Connection();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        user = new javax.swing.JTextField();
        ques = new javax.swing.JTextField();
        rep = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(451, 305));
        setMinimumSize(new java.awt.Dimension(451, 305));
        getContentPane().setLayout(null);

        user.setBackground(null);
        user.setBorder(null);
        user.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                userMouseReleased(evt);
            }
        });
        user.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                userKeyPressed(evt);
            }
        });
        getContentPane().add(user);
        user.setBounds(213, 45, 180, 30);

        ques.setEditable(false);
        ques.setBackground(null);
        ques.setBorder(null);
        ques.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                quesMouseClicked(evt);
            }
        });
        getContentPane().add(ques);
        ques.setBounds(213, 100, 180, 30);

        rep.setBackground(null);
        rep.setBorder(null);
        getContentPane().add(rep);
        rep.setBounds(213, 171, 180, 30);

        jLabel1.setText("Username :");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(80, 50, 81, 15);

        jLabel2.setText("Question :");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(90, 110, 73, 15);

        jLabel3.setText("Réponse :");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(80, 170, 71, 15);

        jLabel4.setText("Remettre");
        jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jLabel4MouseReleased(evt);
            }
        });
        getContentPane().add(jLabel4);
        jLabel4.setBounds(210, 253, 104, 27);

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt1.png"))); // NOI18N
        getContentPane().add(jLabel6);
        jLabel6.setBounds(200, 30, 220, 60);

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt1.png"))); // NOI18N
        getContentPane().add(jLabel7);
        jLabel7.setBounds(200, 90, 220, 50);

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt1.png"))); // NOI18N
        getContentPane().add(jLabel8);
        jLabel8.setBounds(200, 160, 220, 50);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void quesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_quesMouseClicked
       
        
        getQuestion();
        
    }//GEN-LAST:event_quesMouseClicked
    
    private void getQuestion()
    {
         try {
            
            String username=user.getText();
            String sql_question="select * from utilisateur where LOGIN='"+username+"'";
            
            
            PreparedStatement ps_ques=conn.prepareStatement(sql_question);
            
           rs=ps_ques.executeQuery();
           while(rs.next()){
           
                String question=rs.getString("QUESTION");
                ques.setText(question);
           }
            
            
            
        } catch (Exception e) {
        }
    
    }
    
    
    private void userMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_userMouseReleased
       
    }//GEN-LAST:event_userMouseReleased

    private void jLabel4MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel4MouseReleased
        
        try {
            
            String username=user.getText();
            String repon=rep.getText();
            
            String sql_question="select * from utilisateur where LOGIN='"+username+"'";
            //String sql_reponse="select * from utilisateur where LOGIN='"+username+"'";
            PreparedStatement ps_ques=conn.prepareStatement(sql_question);
            //PreparedStatement ps_rep=conn.prepareStatement(sql_reponse);
           rs=ps_ques.executeQuery();
           ResultSet rs1;
           //rs1=ps_rep.executeQuery();
           if(rs.next()){
           
                    //String question=rs.getString("QUESTION");
                
                    
                    String reponse=rs.getString("REPONSE");
                    
                    if(repon.equals(reponse)){
                    
                        Utilisateurs util = new Utilisateurs();
                        util.Modifier();
                        util.setVisible(true);
                        setVisible(false);
                        
                         JOptionPane.showMessageDialog(null, "Réponse Correct!");
                         
                    }
                    else{
                        
                        JOptionPane.showMessageDialog(null, "la réponse est incorrect !");
                    }
                   
               
           }
            
            
        } catch (SQLException | HeadlessException e) {
        }
        
        
    }//GEN-LAST:event_jLabel4MouseReleased

    private void userKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_userKeyPressed
        
        
        getQuestion();
        
        
    }//GEN-LAST:event_userKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(remettre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(remettre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(remettre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(remettre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new remettre().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField ques;
    private javax.swing.JTextField rep;
    private javax.swing.JTextField user;
    // End of variables declaration//GEN-END:variables
}
